package com.hdoit.ares.web.cust.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.viewmodel.cust.CustomerSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月14日 15:55:02
 * 
 */
@Controller
@RequestMapping(value = "/admin/cust/personRelation/")
public class PersonRelationController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/cust/personRelation/";
	}

	private ICustomerService customerService;

	public ICustomerService getCustomerService() {
		return customerService;
	}

	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "customerDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	private final String secondIndexViewName = "secondIndex";
	private final String secondOutputViewName = "secondOutput";

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			CustomerSearch search, boolean ajaxPost) {

		/*String key = getLoginModel().getLoginId();*/
		String key = "0cb9e2d6-69ae-4e61-ab29-a15aa4055ca8";
		pagerModel.setTotalAmount(customerService.queryCountById(search,key));
		List<Customer> customers = customerService.queryListById(search,
				pagerModel,key);

		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("customers", customers);
		model.addAttribute("customerSearch", search);

		if (ajaxPost) {
			return view(outputViewName);
		}

		return view(indexViewName);
	}
	
	@RequestMapping(value = secondIndexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String secondIndex(Model model,String key, PagerModel pagerModel,
			CustomerSearch search, boolean ajaxPost) {	
		pagerModel.setTotalAmount(customerService.queryCountById(search,key));
		List<Customer> customers = customerService.queryListById(search,
				pagerModel,key);

		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("customers", customers);
		model.addAttribute("customerSearch", search);
		model.addAttribute("key", key);

		if (ajaxPost) {
			return view(secondOutputViewName);
		}

		return view(secondIndexViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String customerDetailed(Model model, String key) {
		Customer customer = new Customer();
		if (!StringUtils.isEmpty(key)) {
			customer = customerService.queryModelMP(key);
		}
		model.addAttribute("customer", customer);
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, Customer customer) {
		if (StringUtils.isEmpty(customer.getId())) {
			customer.setId(CommonUtils.getKeyId());
			customer.setModelStatus(ModelStatus.add);
		} else {
			customer.setModelStatus(ModelStatus.update);
		}

		ActionResult actionResult;
		if (customerService.applyChange(customer)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}

		model.addAttribute("customer", customer);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);

		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (customerService.deleteFakeByKey(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}

}
